<template>
  <el-dropdown trigger="click" @command="handleSetSize">
    <div>
      <i class="el-icon-rank" />
    </div>
    <el-dropdown-menu slot="dropdown">
      <el-dropdown-item :disabled="size === 'medium'" command="medium">Medium</el-dropdown-item>
      <el-dropdown-item :disabled="size === 'small'" command="small">Small</el-dropdown-item>
      <el-dropdown-item :disabled="size === 'mini'" command="mini">Mini</el-dropdown-item>
    </el-dropdown-menu>
  </el-dropdown>
</template>

<script>
export default {
  computed: {
    // 通过 getter 获取当前布局尺寸
    size() {
      return this.$store.getters.size;
    },
  },
  methods: {
    handleSetSize(size) {
      // 设置 Element UI 全局尺寸
      this.$ELEMENT.size = size;
      // 调用 settings 模块的 setSize 动作
      this.$store.dispatch('settings/setSize', size);
      this.refreshView();
      this.$message({
        message: '布局尺寸切换成功',
        type: 'success',
      });
    },
    refreshView() {
      // 清空所有缓存视图
      this.$store.dispatch('tagsView/delAllCachedViews', this.$route);
      const { fullPath } = this.$route;
      this.$nextTick(() => {
        // 重定向到当前路由以刷新视图
        this.$router.replace({
          path: '/redirect' + fullPath,
        });
      });
    },
  },
};
</script>

<style scoped>
.el-icon-rank {
  display: inline-block;
  cursor: pointer;
  fill: #5a5e66;
  width: 20px;
  height: 20px;
  font-size: 20px;
}
</style>